#只读
def use_open_r():
    file = open('file2','r',encoding = 'utf8')
    txt = file.read()
    print(txt)
    file.close() #不关闭的话会占用内存空间

#读写(开头读，尾部写,覆盖着写
def use_open_rw():
    file = open('file2', 'r+', encoding='utf8')
    nums = file.write('HELLOmydear') #num表示写入的字符数
    print(nums)
    file.close()

def use_open_a(): #覆盖整个文件从头开始写，未发现文件则报错
    file = open('file2', 'r+', encoding='utf8')
    nums = file.write('123') #num表示写入的字符数
    print(nums)
    file.close()

def use_open_w(): #不存在则被创建，存在则被清空
    # file = open('dir1/file3', 'w', encoding='utf8')
    file = open('file2', 'w', encoding='utf8')
    nums = file.write('12345656753') #num表示写入的字符数
    print(nums)
    file.close()

use_open_r()
use_open_rw()
use_open_r()
print('*' *50)
use_open_a()
use_open_r()
print('*' *50)
use_open_a()
use_open_w()
use_open_r()
